package com.live.dengdengtv.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppConstants;
import com.live.dengdengtv.bean.gson.ConversationSp;
import com.live.dengdengtv.bean.gson.NoticeSp;
import com.live.dengdengtv.utils.EmptyViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyf on 2016/10/21.
 */
public class NoticeFrag extends BaseFragment{
    private static final String TAG = "NoticeFrag";

    private View conView;
    private ListView listView;
    private BaseAdapter adapter;
    private List<NoticeSp.NoticeItem> ListViewData = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(conView == null) {
            listView = getNoticeView();
            adapter = new NoticeAdapter();
            listView.setAdapter(adapter);

//            View emptyView = EmptyViewUtil.getEmptyView(getContext(),"这里什么都没有哦~");
//            container.addView(emptyView);
//            listView.setEmptyView(emptyView);
            getNoticeData();
            if(ListViewData.size() == 0){
                conView = inflater.inflate(R.layout.fragment_blank,container,false);
            }else {
                conView = listView;
            }
        }
        return conView;
    }

    @Override
    public void onResume() {
        super.onResume();
//        View emptyView = EmptyViewUtil.getEmptyView(getContext(),"这里什么都没有哦~");
//        ((ViewGroup)listView.getParent()).addView(emptyView);
//        listView.setEmptyView(emptyView);
    }

    private void getNoticeData() {
        SharedPreferences sp = getActivity().getSharedPreferences(AppConstants.SP_CONVERSATIOM, Context.MODE_PRIVATE);
        String conJson = sp.getString(AppConstants.KEY_NOTICE, "");

        if (!TextUtils.isEmpty(conJson)) {
            Log.e(TAG, "有通知");
            NoticeSp noticeSp = new Gson().fromJson(conJson, NoticeSp.class);
            ListViewData = noticeSp.noticeList;
        }else {
            Log.e(TAG, "通知为空");
        }
    }
    private ListView getNoticeView() {
        ListView listView = new ListView(getActivity());
        ViewPager.LayoutParams lp = new ViewPager.LayoutParams();
        lp.height = lp.MATCH_PARENT;
        lp.width = lp.MATCH_PARENT;
        listView.setLayoutParams(lp);
        listView.setDivider(new ColorDrawable(0xFFE5E5E5));
        listView.setDividerHeight(2);
        return listView;
    }

    private class NoticeAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return ListViewData.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView == null){
                convertView = View.inflate(getActivity(), R.layout.card_notice, null);

                viewHolder = new ViewHolder();
                viewHolder.msg_tv = (TextView) convertView.findViewById(R.id.msg_tv);
                viewHolder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.msg_tv.setText(ListViewData.get(position).msg);
            viewHolder.time_tv.setText(ListViewData.get(position).time);
            return convertView;
        }
    }

    private static class ViewHolder{
        TextView msg_tv;
        TextView time_tv;
    }


}
